package cn.tsmchina.one.controller.activity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.tsmchina.one.domain.activity.Activity;
import cn.tsmchina.one.pojo.ActivityPojo;
import cn.tsmchina.one.service.activity.ActivityService;
import cn.tsmchina.one.service.user.CustomerService;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.LoginUtil;
import cn.tsmchina.one.util.Return;

@Controller
@RequestMapping("/activity")
public class ActivityController
{
	@Autowired
	private ActivityService activityService;
	@Autowired
	private CustomerService customerService;
	/**
	 * 添加活动信息
	 */
	 	/**
	 * 添加活动信息
	 */
	 	/**
	 * 添加活动信息
	 */
	@RequestMapping("/add")
	public String add()
	{
		return "activity/addActivity";
	}
	
	/**
	 * 添加活动信息提交
	 * @param activity
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add",method= RequestMethod.POST)
	public String addActivity(@ModelAttribute Activity activity,HttpServletRequest request)
	{
		try
		{	
//			long cid = LoginUtil.getCustomerId(request);
//			activity.setCustomerId(cid);
			activityService.addActivity(activity);
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "添加成功!");
			request.setAttribute("return",request.getContextPath()+"/activity/query");
		} catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "添加失败!");
			request.setAttribute("return",request.getContextPath()+"/activity/query");
		}
		
		return Return.redirect(request);
	}
	
	/**
	 * 活动列表信息查询
	 * @param model
	 * @return
	 */
	@RequestMapping("/query")
	public String activity(Model model,HttpServletRequest request)
	{
		long cid = LoginUtil.getCustomerId(request, customerService);
		if(cid==-1) {
			return "m/login";
		}
		int page = 1;
		String curPage = request.getParameter("page");
		if(!StringUtils.isEmpty(curPage)){
			page = Integer.valueOf(curPage);
		}
		this.activity(model, page, cid);
		
		return "activity/queryActivity";
	}
	
	/**
	 * 修改活动信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable("id") int id,HttpServletRequest request)
	{
		Activity activity = activityService.getActivity(id);
		request.setAttribute("activity", activity);
		request.setAttribute("returnUrl",Return.encode(request.getHeader("referer")));
		return "activity/editActivity";
	}
	
	/**
	 * 提交修改活动信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	public String edit(Activity activity,HttpServletRequest request)
	{
		try
		{
			activityService.updateActivity(activity);
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "修改成功!");
			request.setAttribute("return",request.getParameter("return"));
		} catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "修改失败!");
			request.setAttribute("return",request.getParameter("return"));
		}
		return Return.redirect(request);
	}
	
	/**
	 * 查看与会人员
	 */
	@RequestMapping("/view/{id}")
	public String veiw(@PathVariable("id") int id,HttpServletRequest request){
		int page = 1;
		String curPage = request.getParameter("page");
		if(!StringUtils.isEmpty(curPage)){
			page = Integer.valueOf(curPage);
		}
		long customerId = LoginUtil.getCustomerId(request, customerService);
		Map<String, Object> map = activityService.viewJoinsById(id,page,customerId);
		
		request.setAttribute("total", map.get("total"));
		request.setAttribute("pageTotal", map.get("pageTotal"));
		request.setAttribute("page", page);
		request.setAttribute("len", map.get("len"));
		request.setAttribute("total", map.get("total"));
		request.setAttribute("users", map.get("users"));
		request.setAttribute("activity", map.get("activity"));
		
		return "activity/viewActivity";
	}
	
	/**
	 * 删除活动信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable("id") int id,HttpServletRequest request)
	{
		try
		{
			activityService.deleteActivityById(id);
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "删除成功!");
			request.setAttribute("return", request.getHeader("referer"));
		} catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "删除失败!");
			request.setAttribute("return", request.getHeader("referer"));
		}
		return Return.redirect(request);
	}
	
	/**
	 * 活动管理
	 * @author  YUE
	 * @date 2013-8-30 下午2:06:21
	 * @return
	 */
	private void activity(Model model,int page, long cid)
	{
		try
		{
			page = page <= 0 ? 1 : page;
			List<ActivityPojo> list = activityService.getActivities(page, cid);
			int total = activityService.getTotal(cid);
			int pageTotal = total % C.Paging.SIZE == 0 ? total / C.Paging.SIZE : total / C.Paging.SIZE + 1;
			model.addAttribute("total", total);
			model.addAttribute("pageTotal", pageTotal);
			model.addAttribute("page",page);
			model.addAttribute("activitys", list);
			model.addAttribute("len", list.size());
		} catch (Exception e)
		{
			model.addAttribute("len", 0);
		}
	}
	
	/**
	 * 活动详情
	 * @param model
	 * @return
	 */
	@RequestMapping("/activity/detail/{id}")
	public String activityDetail(@PathVariable("id")int id,HttpServletRequest request)
	{
		Activity activity = activityService.getActivity(id);
		request.setAttribute("activity", activity);
		request.setAttribute("returnUrl",Return.encode(request.getHeader("referer")));
		return "activityDetail";
	}
	
	/**
	 * 活动信息
	 * @author  YUE
	 * @date 2013-8-30 下午2:06:41
	 * @param page
	 * @param model 
	 * @return
	 */
	@RequestMapping("/activity/{page}")
	public String activityList(@PathVariable("page") int page, Model model, HttpServletRequest request)
	{
		long cid = LoginUtil.getCustomerId(request, customerService);
		if(cid==-1) {
			return "m/login";
		}
		this.activity(model, page, cid);
		return "activity";
	}
	
	@InitBinder
	public void convertDate(WebDataBinder binder)
	{
		CustomDateEditor editor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), false);
		binder.registerCustomEditor(Date.class,editor);
	}
	
	@RequestMapping(value="/activity/add", method = RequestMethod.POST)
	public String add(@ModelAttribute Activity activity,HttpServletRequest request)
	{
		try
		{
//			long cid = LoginUtil.getCustomerId(request);
//			activity.setCustomerId(cid);
			activityService.addActivity(activity);
			request.setAttribute("msgType", C.Msg.OK);
			request.setAttribute("msg", "添加成功!");
			request.setAttribute("return",request.getContextPath()+"/activity");
		} catch (Exception e)
		{
			request.setAttribute("msgType", C.Msg.ERROR);
			request.setAttribute("msg", "添加失败!");
			request.setAttribute("return",request.getContextPath()+"/activity");
		}
		return Return.redirect(request);
	}
	
}
